package com.shujia.mapredue;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class Demo2WordCount {


    public static class WordCountMapper extends Mapper<LongWritable, Text, Text, LongWritable> {
         /**
         * map 方法，每一行数据都会执行一次
         */
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

            //获取一行数据
            String line = value.toString();

            //一行包含多个单词
            String[] words = line.split(",");

            for (String word : words) {
                //将数据输出到reduce端
                //同一个key会被分到同一个reduce中
                context.write(new Text(word), new LongWritable(1));
            }
        }
    }


    public static class WordCountReduce extends Reducer<Text, LongWritable, Text, LongWritable> {

        /**
         * reduce 方法，每一个key调用一次
         * <p>
         * key： 一个单词
         * values ; 一个单词所有的值 （很多个1）
         */

        @Override
        protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {


            //累加统计单词的数量
            Long sum = 0L;
            for (LongWritable value : values) {
                sum = sum + value.get();
            }

            //单词
            String word = key.toString();


            //将数据输出到hdfs
            context.write(new Text(word), new LongWritable(sum));

        }
    }

    /**
     * 在main函数中构建mapreduce程序
     */

    public static void main(String[] args) throws Exception {

        //构建一个job
        Job job = Job.getInstance();

        //指定任务名
        job.setJobName("wc");

        //指定reduce数量，默认是一个
        job.setNumReduceTasks(2);


        //指定main所在类的类名
        job.setJarByClass(Demo2WordCount.class);


        //指定map端类
        job.setMapperClass(WordCountMapper.class);

        //指定map输出的key和value的类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);

        //指定reduce端的类
        job.setReducerClass(WordCountReduce.class);

        ///指定reduce输出的key和value的类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);


        //指定输入路径，hdfs上的一个文件或者目录
        //先将words.txt 上传到hdfs    hadoop dfs -put words.txt /data
        FileInputFormat.addInputPath(job, new Path("/data/words/"));


        Path outPath = new Path("/data/wc");
        FileSystem fileSystem = FileSystem.get(new Configuration());
        //如果输出路径已存在，先删除
        if (fileSystem.exists(outPath)) {
            fileSystem.delete(outPath, true);
        }

        //指定输出路径，  输出路径只能是一个目录
        FileOutputFormat.setOutputPath(job, outPath);


        //启动任务
        job.waitForCompletion(true);
        /**
         *
         * 提交任务
         * 1、通过maven将项目打包，上传到服务器
         * 2、提交任务，指定jar包  指定类全名
         *  hadoop jar hadoop-1.0.jar  com.shujia.mapredue.Demo2WordCount
         *
         * 3、 查看结果
         *  hadoop dfs -cat /data/wc/*
         *
         *  默认只有一个reduce所有只生成了一个文件
         *
         *
         */
    }
}
